package cn.dnui_xqh414.dnuifood_16110100414.fragment_adapter_and_vhoder;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.snackbar.Snackbar;
import com.squareup.picasso.Picasso;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import cn.dnui_xqh414.dnuifood_16110100414.FoodDetailActivity;
import cn.dnui_xqh414.dnuifood_16110100414.R;
import cn.dnui_xqh414.dnuifood_16110100414.bean.CollectBean;
import cn.dnui_xqh414.dnuifood_16110100414.bean.Collectstate;
import cn.dnui_xqh414.dnuifood_16110100414.bean.ShopdetailBean;
import cn.dnui_xqh414.dnuifood_16110100414.listener.OnRetrofitListener;
import cn.dnui_xqh414.dnuifood_16110100414.model.ShopModel;

import static android.content.Context.MODE_PRIVATE;

public class rv_Adapter_shopList
        extends RecyclerView.
        Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<ShopdetailBean> list;
    private LayoutInflater inflater;

    private ShopModel shopModel = new ShopModel();
    private String favflag = "-1";

    final private String[] titles = new String[]{"1","1","1","1","1","1","1","1","1","1","1"};
    private String phonenum;

    public rv_Adapter_shopList(Context context, String phonenum){
        this.context = context;
        this.phonenum = phonenum;
        inflater = LayoutInflater.from(context);
    }

    public void setList(List<ShopdetailBean> list){
        this.list = list;

        notifyDataSetChanged();   //十分重要
    }

    class shopFoodListViewHolder extends
            RecyclerView.ViewHolder {

         View itemView;

         TextView titleName,foodIntro,foodPrice;
         ImageView imageView,recomment;

         ImageButton imgb_cart,imgb_collect;

        public shopFoodListViewHolder(@NonNull View itemView) {
            super(itemView);
            titleName = itemView.findViewById(R.id.tv_foodList_name);
            imageView = itemView.findViewById(R.id.img_foodList);
            foodIntro = itemView.findViewById(R.id.tv_food_list_intro);
            foodPrice = itemView.findViewById(R.id.tv_food_price);
            imgb_collect = itemView.findViewById(R.id.imgb_food_star);
            imgb_cart = itemView.findViewById(R.id.imgbt_shopcar);
            recomment = itemView.findViewById(R.id.img_reComment);

            this.itemView = itemView;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.rv_shop_food_list,
                        parent ,false);
        return new shopFoodListViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {

        final ShopdetailBean shopdetailBean = list.get(position);
        final shopFoodListViewHolder viewHolder = (shopFoodListViewHolder) holder;
//        for (String title:titles)
//        viewHolder.titleName.setText(title);

        if (shopdetailBean == null) return;

        viewHolder.titleName.setText(shopdetailBean.getFoodname());
        if(shopdetailBean.getIntro().equals(""))
            viewHolder.foodIntro.setText("这个food真懒，没有写intro");
        else
            viewHolder.foodIntro.setText(shopdetailBean.getIntro());
        viewHolder.foodPrice.setText("￥"+String.valueOf(shopdetailBean.getPrice()));

        if (shopdetailBean.getRecommand() != 1){
            viewHolder.recomment.setAlpha((float) 0);
        }

        viewHolder.imgb_cart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v,"功能还未实现",Snackbar.LENGTH_SHORT).show();
            }
        });

        SharedPreferences sharedPreferences = context.getSharedPreferences("userinfo",MODE_PRIVATE);
        int userid = sharedPreferences.getInt("userid",-1);
        shopModel.getCollectstate(userid, shopdetailBean.getFood_id(), 1, new OnRetrofitListener<Collectstate>() {
            @Override
            public void onSuccess(Collectstate object, int flag) {
                if (object.getCollected().equals("1"))
                {
                    viewHolder.imgb_collect.setBackground(context.getDrawable(R.drawable.ic_food_star_filled));
                    favflag = "1";
                }else {
                    viewHolder.imgb_collect.setBackground(context.getDrawable(R.drawable.ic_food_star));
                    favflag = "0";
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });

        viewHolder.imgb_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                SharedPreferences sharedPreferences = context.getSharedPreferences("userinfo",MODE_PRIVATE);
                int userid = sharedPreferences.getInt("userid",-1);
                shopModel.doFoodListCollect(userid, shopdetailBean.getFood_id(), new OnRetrofitListener<CollectBean>() {
                    @Override
                    public void onSuccess(CollectBean object, int flag) {
                        if (object.getSuccess().equals("1"))
                        {
                            if (favflag.equals("1"))
                            {
                                viewHolder.imgb_collect.setBackground(context.getDrawable(R.drawable.ic_food_star));
                                favflag = "0";
                                Toast.makeText(context, "UnStar!", Toast.LENGTH_SHORT).show();
                            }else {
                                favflag = "1";
                                viewHolder.imgb_collect.setBackground(context.getDrawable(R.drawable.ic_food_star_filled));
                                Toast.makeText(context, "Star!", Toast.LENGTH_SHORT).show();
                            }
                        }else {
                            Toast.makeText(context, "Unknow error", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });


        String url =
                "http://172.24.10.175:8080/foodService/"+shopdetailBean.getPic();
        Picasso.get().load(url).placeholder(R.drawable.ic_loading)
                .error(R.drawable.ic_load_fail_sfont)
                .fit().centerCrop().into(viewHolder.imageView);

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                Context context = v.getContext();
                intent.putExtra("foodid", shopdetailBean.getFood_id());
                intent.putExtra("shopid",shopdetailBean.getShop_id());
                intent.putExtra("phonenum",phonenum);
                intent.setClass(context, FoodDetailActivity.class);
                context.startActivity(intent);

            }
        });


    }

    @Override
    public int getItemCount() {
        if (list == null)
        return 0;
        return list.size();
    }
}
